package demo3;

public class Test9 {
    public static void func(Animal animal){
        animal.eat();
    }
    public static void main(String [] args){
        Animal animal=new Bird("小鸟",2);//先向上转型
        Bird bird=(Bird)animal;//被父类对象引用过的对象才可以向下转型，且转型时要强制类型转换，否则无法成功
        bird.fly();
        Animal animal12=new Dog("坤哥",2);
        animal12.eat();
        if(animal12 instanceof Dog){//用instanceof关键字判断animal是否引用了Dog这个对象，引用后才可向下转型
            Dog dog2=(Dog)animal12 ;
            dog2.bark();
        }else {
            System.out.println("animal12 instanceof Dog not!!!!! ");
        }
      Dog dog=new Dog("哈哈",3);//向上转型
        func(dog);

    }
}
